import { Router } from 'express';
import { CreditController } from '../controllers/CreditController';

const router: Router = Router();
const creditController = new CreditController();

/**
 * @swagger
 * components:
 *   schemas:
 *     CreditPackage:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 积分包ID
 *         name:
 *           type: string
 *           description: 积分包名称
 *         price:
 *           type: number
 *           description: 价格
 *         credits:
 *           type: integer
 *           description: 积分数量
 *         validDays:
 *           type: integer
 *           description: 有效天数
 *         features:
 *           type: array
 *           items:
 *             type: string
 *           description: 功能特性列表
 *         popular:
 *           type: boolean
 *           description: 是否为推荐包
 *     UserCreditInfo:
 *       type: object
 *       properties:
 *         userId:
 *           type: integer
 *           description: 用户ID
 *         creditBalance:
 *           type: integer
 *           description: 积分余额
 *         expiresAt:
 *           type: string
 *           format: date-time
 *           description: 过期时间
 *         daysUntilExpiration:
 *           type: integer
 *           description: 距离过期天数
 *         isExpired:
 *           type: boolean
 *           description: 是否已过期
 *     CreditTransaction:
 *       type: object
 *       properties:
 *         id:
 *           type: string
 *           description: 交易ID
 *         userId:
 *           type: integer
 *           description: 用户ID
 *         transactionType:
 *           type: string
 *           enum: [purchase, usage, refund, expiration]
 *           description: 交易类型
 *         creditAmount:
 *           type: integer
 *           description: 积分数量（正数为充值，负数为使用）
 *         description:
 *           type: string
 *           description: 交易描述
 *         referenceId:
 *           type: string
 *           description: 关联ID（如支付ID）
 *         metadata:
 *           type: object
 *           description: 元数据
 *         createdAt:
 *           type: string
 *           format: date-time
 *           description: 创建时间
 */

/**
 * @swagger
 * /api/v1/credits/packages:
 *   get:
 *     tags: [Credits]
 *     summary: 获取积分包列表
 *     description: 获取所有可用的积分包
 *     responses:
 *       200:
 *         description: 成功获取积分包列表
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/CreditPackage'
 *                 message:
 *                   type: string
 *                   example: 获取积分包列表成功
 */
router.get('/packages', creditController.getCreditPackages);

/**
 * @swagger
 * /api/v1/credits/users/{userId}:
 *   get:
 *     tags: [Credits]
 *     summary: 获取用户积分信息
 *     description: 获取指定用户的积分余额和过期时间
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     responses:
 *       200:
 *         description: 成功获取用户积分信息
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/UserCreditInfo'
 *                 message:
 *                   type: string
 *                   example: 获取用户积分信息成功
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.get('/users/:userId', creditController.getUserCredits);

/**
 * @swagger
 * /api/v1/credits/users/{userId}/use:
 *   post:
 *     tags: [Credits]
 *     summary: 使用积分
 *     description: 消费用户积分
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - credits
 *             properties:
 *               credits:
 *                 type: integer
 *                 minimum: 1
 *                 description: 使用的积分数量
 *               description:
 *                 type: string
 *                 description: 使用描述
 *               referenceId:
 *                 type: string
 *                 description: 关联ID
 *               metadata:
 *                 type: object
 *                 description: 元数据
 *     responses:
 *       200:
 *         description: 积分使用成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/UserCreditInfo'
 *                 message:
 *                   type: string
 *                   example: 积分使用成功
 *       400:
 *         description: 积分不足或请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.post('/users/:userId/use', creditController.useCredits);

/**
 * @swagger
 * /api/v1/credits/users/{userId}/transactions:
 *   get:
 *     tags: [Credits]
 *     summary: 获取用户积分交易历史
 *     description: 获取指定用户的积分充值和使用记录
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           default: 20
 *         description: 每页数量
 *       - in: query
 *         name: offset
 *         schema:
 *           type: integer
 *           default: 0
 *         description: 偏移量
 *     responses:
 *       200:
 *         description: 成功获取积分交易历史
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/CreditTransaction'
 *                 message:
 *                   type: string
 *                   example: 获取积分交易历史成功
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.get('/users/:userId/transactions', creditController.getUserCreditTransactions);

/**
 * @swagger
 * /api/v1/credits/payment-success:
 *   post:
 *     tags: [Credits]
 *     summary: 处理支付成功后的积分添加
 *     description: 支付成功后调用此接口添加对应的积分
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - userId
 *               - packageId
 *               - paymentIntentId
 *             properties:
 *               userId:
 *                 type: integer
 *                 description: 用户ID
 *               packageId:
 *                 type: string
 *                 description: 积分包ID
 *               paymentIntentId:
 *                 type: string
 *                 description: Stripe支付意图ID
 *     responses:
 *       200:
 *         description: 积分添加成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/UserCreditInfo'
 *                 message:
 *                   type: string
 *                   example: 积分添加成功
 *       400:
 *         description: 请求参数错误或积分包不存在
 *       500:
 *         description: 服务器错误
 */
router.post('/payment-success', creditController.handlePaymentSuccess);

/**
 * @swagger
 * /api/v1/credits/users/{userId}/add:
 *   post:
 *     tags: [Credits]
 *     summary: 管理员手动添加积分
 *     description: 管理员为用户手动添加积分（需要管理员权限）
 *     parameters:
 *       - in: path
 *         name: userId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 用户ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - credits
 *               - validDays
 *             properties:
 *               credits:
 *                 type: integer
 *                 minimum: 1
 *                 description: 添加的积分数量
 *               validDays:
 *                 type: integer
 *                 minimum: 1
 *                 description: 有效天数
 *               description:
 *                 type: string
 *                 description: 添加说明
 *               referenceId:
 *                 type: string
 *                 description: 关联ID
 *               metadata:
 *                 type: object
 *                 description: 元数据
 *     responses:
 *       200:
 *         description: 积分添加成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   $ref: '#/components/schemas/UserCreditInfo'
 *                 message:
 *                   type: string
 *                   example: 积分添加成功
 *       400:
 *         description: 请求参数错误
 *       500:
 *         description: 服务器错误
 */
router.post('/users/:userId/add', creditController.addCredits);

/**
 * @swagger
 * /api/v1/credits/admin/expire:
 *   post:
 *     tags: [Credits]
 *     summary: 管理员清理过期积分
 *     description: 检查并清理所有过期的积分记录（管理员功能）
 *     responses:
 *       200:
 *         description: 清理成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 success:
 *                   type: boolean
 *                   example: true
 *                 data:
 *                   type: object
 *                   properties:
 *                     expiredCount:
 *                       type: integer
 *                       description: 清理的过期记录数量
 *                 message:
 *                   type: string
 *                   example: 成功清理 5 个过期积分记录
 *       500:
 *         description: 服务器错误
 */
router.post('/admin/expire', creditController.expireCredits);

export default router; 